package com.alks.yn_zs.service.serviceImpl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSON;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.utils.WeekdayTranslator;
import com.alks.common.utils.beanutils.StringUtils;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.yn_zs.data.dto.FinishStoreDTO;
import com.alks.yn_zs.data.dto.FinishStoreDailyDTO;
import com.alks.yn_zs.data.dto.FinishStoreInOutDTO;
import com.alks.yn_zs.data.vo.FinishStoreVO;
import com.alks.yn_zs.data.vo.InOutStatisticVO;
import com.alks.yn_zs.mapper.FinishStoreMapper;
import com.alks.yn_zs.service.IFinishStoreService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: chen
 * @date: 2024/11/22
 * @description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FinishStoreServiceImpl implements IFinishStoreService {

    final private FinishStoreMapper finishStoreMapper;

    @Override
    @AutoPageAop
    public PageRecord<FinishStoreVO> pageDetail(PageParam pageParam) {
        List<FinishStoreDTO> list = finishStoreMapper.listDetail(pageParam);
        List<FinishStoreVO> res = list.stream().map(FinishStoreVO::convert).collect(Collectors.toList());
        return new PageRecord<>(res);
    }

    @Override
    public InOutStatisticVO inOutStatistic(String timeStr) {
        InOutStatisticVO dto = new InOutStatisticVO();
        LocalDateTime currentTime = null;
        if (!StringUtils.isEmpty(timeStr)) {
            try {
                currentTime = LocalDateTimeUtil.parseDate(timeStr).atStartOfDay();
            } catch (Exception ex) {
                throw new ServiceErrorException("参数异常");
            }
        } else {
            currentTime = LocalDateTime.now();
        }
        // 查询今日汇总
        FinishStoreInOutDTO dailyDataDTO = finishStoreMapper.summaryByTime(
                LocalDateTimeUtil.beginOfDay(currentTime), LocalDateTimeUtil.endOfDay(currentTime));
        Long totalStore = finishStoreMapper.summaryStore();
        InOutStatisticVO.DataItem dailyData = new InOutStatisticVO
                .DataItem(dailyDataDTO.getInQty(), dailyDataDTO.getOutQty());
        dailyData.setStore(totalStore);
        dto.setDaily(dailyData);
        // 查询本月汇总
        LocalDateTime firstDayOfMonth = currentTime.with(TemporalAdjusters.firstDayOfMonth());
        FinishStoreInOutDTO monthlyDataDTO = finishStoreMapper.summaryByTime(
                LocalDateTimeUtil.beginOfDay(firstDayOfMonth), LocalDateTimeUtil.endOfDay(currentTime));
        InOutStatisticVO.DataItem monthlyData = new InOutStatisticVO
                .DataItem(monthlyDataDTO.getInQty(), monthlyDataDTO.getOutQty());
        dto.setMonthly(monthlyData);
        // 查询近七日汇总
//        LocalDateTime beginTime = LocalDateTimeUtil.beginOfDay(currentTime.minusDays(6));
//        List<FinishStoreDailyDTO> recentlyData = finishStoreMapper.summaryDaily(
//                beginTime, LocalDateTimeUtil.endOfDay(currentTime));
        // 2025年10月23日 修改为获取当月数据
        // 获取当月最后一天
        LocalDateTime lastDayOfMonth = currentTime
                .with(TemporalAdjusters.lastDayOfMonth());
        List<FinishStoreDailyDTO> recentlyData = finishStoreMapper.summaryDaily(firstDayOfMonth, lastDayOfMonth);
        Map<LocalDate, FinishStoreDailyDTO> map = recentlyData.stream().collect(Collectors.toMap(FinishStoreDailyDTO::getCollectDate, v -> v));

        List<InOutStatisticVO.TimeData> timeDataList = new ArrayList<>();
        LocalDate currentDate = firstDayOfMonth.toLocalDate();
        while (!currentDate.isAfter(lastDayOfMonth.toLocalDate())) {
            FinishStoreDailyDTO dailyDTO = map.get(currentDate);
            InOutStatisticVO.TimeData timeData = new InOutStatisticVO.TimeData();
            if (dailyDTO == null) {
                timeData.setDate(currentDate);
                timeData.setWeek(currentDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));
                timeData.setWeekCn(WeekdayTranslator.translateEnToCn(timeData.getWeek()));
                timeData.setData(new InOutStatisticVO.DataItem(0L, 0L));
            } else {
                timeData.setDate(dailyDTO.getCollectDate());
                timeData.setWeek(dailyDTO.getDateOfWeek());
                timeData.setWeekCn(WeekdayTranslator.translateEnToCn(dailyDTO.getDateOfWeek()));
                timeData.setData(new InOutStatisticVO.DataItem(dailyDTO.getInQty(), dailyDTO.getOutQty()));
            }
            timeDataList.add(timeData);
            currentDate = currentDate.plusDays(1);
        }
        dto.setRecently(timeDataList);
        return dto;
    }
}
